package com.wl156.activities;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.xmlpull.v1.XmlPullParserException;

import android.os.AsyncTask;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;

import com.wl156.BaseActivity;
import com.wl156.MyApp;
import com.wl156.lite.R;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;
import com.wl156.utils.Utils;

public class ShangmaoDetail extends BaseActivity {

	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;

	TextView mTitle;
	TextView mFrom;
	TextView mPublish;
	WebView mContent;

	private String newsID;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.act_shangmao_detail);

		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

		newsID = getIntent().getStringExtra("id");

		mTitle = (TextView) findViewById(R.id.title);
		mFrom = (TextView) findViewById(R.id.from);
		mPublish = (TextView) findViewById(R.id.publish);
		mContent = (WebView) findViewById(R.id.content);

		new AsyncTask<Void, Void, Object>() {

			protected void onPostExecute(Object result) {
				if (result instanceof Exception) {
					Exception e = (Exception) result;

					Toast.makeText(ShangmaoDetail.this, "加载失败，请稍候再试",
							Toast.LENGTH_LONG).show();
				} else {
					SoapObject so = (SoapObject) result;
					String title = Utils.propertyToShow(so.getProperty(0));
					String from = Utils.propertyToShow(so.getProperty(1));
					String publish = Utils.propertyToShow(so.getProperty(2));
					String content = Utils.propertyToShow(so.getProperty(3));

					mTitle.setText(title);
					mFrom.setText(from);
					mPublish.setText(publish);
					mContent.loadDataWithBaseURL(null, content, "text/html",
							"utf-8", null);
				}
			};

			@Override
			protected Object doInBackground(Void... params) {
				try {
					return mWebServiceClient.common_getNewsView(mCertificate,
							newsID);
				} catch (IOException e) {
					return e;
				} catch (XmlPullParserException e) {
					return e;
				}
			}

		}.execute();
	}
}
